/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StatSeries.java
 *
 * Created on 28 Μαϊ 2010, 3:51:43 μμ
 */
package myseriesproject.statistics;

import com.googlecode.soptions.Option;
import database.DBConnection;
import database.SeriesRecord;
import java.awt.Color;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import myComponents.myTableCellRenderers.MyDecimalFormatRenderer;
import myseriesproject.MySeries;
import tools.MySeriesLogger;
import tools.options.MySeriesOptions;

/**
 *
 * @author ssoldatos
 */
public class StatSeries extends AbstractStat {

  public static int SERIES_COLUMN = 0;
  public static int EPISODES_COLUMN = 1;
  public static int RATE_COLUMN = 2;
  private boolean unifiedSeries = false;

  /**
   * Creates new form StatSeries
   */
  public StatSeries() {
    initComponents();
    create();

  }

  private void create() {
    jScrollPane1.getViewport().setOpaque(false);
    model = (DefaultTableModel) getTable().getModel();
    getTable().getColumnModel().getColumn(RATE_COLUMN).setCellRenderer(new MyDecimalFormatRenderer());
    getTable().getTableHeader().setReorderingAllowed(false);
    setVisible(true);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table_stat_series = new javax.swing.JTable();
        lb_title = new javax.swing.JLabel();
        lb_tip = new javax.swing.JLabel();
        cb_unified = new javax.swing.JCheckBox();

        setOpaque(false);

        jScrollPane1.setOpaque(false);

        table_stat_series.setAutoCreateRowSorter(true);
        table_stat_series.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Series", "Episodes", "Rate"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Integer.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table_stat_series.setName("seriesStats"); // NOI18N
        jScrollPane1.setViewportView(table_stat_series);

        lb_title.setFont(lb_title.getFont().deriveFont(lb_title.getFont().getStyle() | java.awt.Font.BOLD, lb_title.getFont().getSize()+2));
        lb_title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lb_title.setText("Series Ratings");

        lb_tip.setText("Mouse over rate to see a list of the series episodes rates");

        cb_unified.setText("Unified series");
        cb_unified.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        cb_unified.setOpaque(false);
        cb_unified.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_unifiedActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
                    .add(lb_title, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(lb_tip, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                        .add(44, 44, 44)
                        .add(cb_unified, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(lb_title)
                .add(14, 14, 14)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lb_tip)
                    .add(cb_unified))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

  private void cb_unifiedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_unifiedActionPerformed
    if (!cb_unified.isSelected()) {
      refresh(false);
    } else {
      refresh(true);
    }
    unifiedSeries = cb_unified.isSelected();
    MySeries.options.setOption(new Option(MySeriesOptions.UNIFIED_SERIES, Option.BOOLEAN_CLASS, cb_unified.isSelected()), true);
  }//GEN-LAST:event_cb_unifiedActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox cb_unified;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lb_tip;
    private javax.swing.JLabel lb_title;
    private javax.swing.JTable table_stat_series;
    // End of variables declaration//GEN-END:variables

  public void refresh(boolean unified) {
    clearModel();
    ResultSet rs = null;
    try {
      Statement stmt = DBConnection.conn.createStatement();
      String sql;
      String showDeleted = " ";
      if (MySeries.options.getBooleanOption(MySeriesOptions.HIDE_DELETED_SERIES_RATINGS)) {
        showDeleted = "AND series.deleted = " + SeriesRecord.NOT_DELETED;
      }
      if (!unified) {
        sql = "SELECT series.series_ID AS series_ID, series.title AS series, sum(episodes.rate)/count(1) as rate, count(1) as episodes "
                + "FROM series join episodes on series.series_ID = episodes.series_ID "
                + "where episodes.rate > 0 " + showDeleted + " group by series.series_ID order by rate desc";
      } else {
        sql = "SELECT series.series_ID AS series_ID, series.title AS series, sum(episodes.rate)/count(1) as rate, count(1) as episodes "
                + "FROM series join episodes on series.series_ID = episodes.series_ID "
                + "where episodes.rate > 0 " + showDeleted + " group by series.title order by rate desc";
      }
      rs = stmt.executeQuery(sql);
      while (rs.next()) {
        SeriesRecord series = SeriesRecord.queryOne(SeriesRecord.C_SERIES_ID + "=?",
                new String[]{String.valueOf(rs.getInt("series_ID"))}, null);
        int episodes = rs.getInt("episodes");
        double rate = rs.getDouble("rate");
        Object[] data = {unified ? series.getTitle() : series, episodes, rate};
        getModel().addRow(data);
      }
      lb_title.setText("Series Ratings (" + getModel().getRowCount() + " series)");
    } catch (SQLException ex) {
    } catch (NullPointerException ex) {
    } finally {
      try {
        rs.close();
      } catch (SQLException ex) {
        MySeriesLogger.logger.log(Level.SEVERE, "Could not close resultset", ex);
      }
    }
  }

  /**
   * @return the isUnifiedSeries
   */
  public boolean isUnifiedSeries() {
    return unifiedSeries;
  }

  /**
   * @param isUnifiedSeries the isUnifiedSeries to set
   */
  public void setUnifiedSeries(boolean un) {
    this.unifiedSeries = un;
    cb_unified.setSelected(un);
    refresh(unifiedSeries);
  }

  public void setTextColor(Color color) {
    lb_title.setForeground(color);
    lb_tip.setForeground(color);
    cb_unified.setForeground(color);
  }

  @Override
  JTable getTable() {
    return table_stat_series;
  }
}
